package Server.ServerCore.Controller;

import Server.ServerCore.Model.Player.PlayerInstance;
import Server.ServerCore.Model.vo.CardVO;
import Server.ServerCore.Service.DataPacks.packRequest.Hit_requst;
import Server.ServerCore.Service.DataPacks.packRespose.Hit_response;
import Server.ServerCore.Service.Logic.HandleController;
import Server.ServerCore.Service.Logic.RegisterPack;
import Server.ServerCore.Service.service.PlayerManagerService;
import javolution.util.FastList;
import org.jboss.netty.buffer.ChannelBuffer;
import org.springframework.stereotype.Service;

/**
 * @author :石头哥哥<br/>
 *         Project:21Server
 *         Date: 13-4-1
 *         Time: 下午9:56
 *         HitController=22  补牌
 *
 *         当这个数据推送前端，前端收到数据恢复显示要牌！
 */
@Service
public class HitController extends HandleController {
    private static final long serialVersionUID = -5132248842632584444L;

    /**
     * 数据分解具体处理的函数
     *
     * @param buffer buffer
     * @param player player
     */
    @Override
    public void DispatchPack(ChannelBuffer buffer, PlayerInstance player) throws Exception {


        Hit_requst hitRequst= (Hit_requst) parseObject(buffer,Hit_requst.class);
        int circleUnitID=hitRequst.getCircleUnitID();
        int points=0;


        FastList<CardVO> cards=new FastList<CardVO>();
        for (CardVO card:player.getCardVOFastList()){
            if (card.getCircleUnitID()==circleUnitID){
                cards.add(card);
            }
        }

        for (CardVO c:cards){
            if (c.getCardType().getPoint()==0){//A
                points+=1;
            }else{
                points+=c.getCardType().getPoint();
            }
        }

        //找 A
        for (CardVO c:cards){
            if (c.getCardType().getPoint()==0){//A
                points=points+10>21?points:points+10;
                break;
             }
        }
        //当前点数小于21 玩家请求发牌则发牌
         if (points<=21&&points>0){
             //补牌数据包 22  庄家补牌
             CardVO cardVO=new CardVO();
             cardVO.setCircleUnitID(circleUnitID);
             FastList<CardVO> cardVOs=player.getCardVOFastList();
             cardVOs.add(cardVO);
             Hit_response hitResponse=new Hit_response();
             hitResponse.setCircleUnitID(circleUnitID);
             hitResponse.setCard(cardVO.getCardType().getID());
             hitResponse.setCard_colour(cardVO.getCardcolour());
             player.DirectSend(hitResponse.getJsonData(2,2));
         }
        cards.clear();

    }

    /**
     * 通过spring产生处理数据的controller，储存在数组中
     */
    @Override
    public void InitiController() {
        RegisterPack.GAME_CONTROLLERS[22]=this;
    }
}
